package com.cruise.game.controller.game;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.game.GameServiceCreateParam;
import com.cruise.game.model.param.game.GameServiceQueryParam;
import com.cruise.game.model.param.game.GameServiceUpdateParam;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.game.GameServiceListVO;
import com.cruise.game.model.vo.game.GameServiceVO;
import com.cruise.game.service.game.ServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏服务控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "游戏服务相关接口")
@RestController
@RequestMapping("/api/game/services")
public class ServiceController extends BaseController {

    @Resource
    private ServiceService serviceService;

    /**
     * 获取游戏服务列表
     */
    @ApiOperation("获取游戏服务列表")
    @GetMapping
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<PageResult<GameServiceListVO>> getServiceList(
            @ApiParam(value = "分类ID", required = false) @RequestParam(value = "categoryId", required = false) Long categoryId,

            @ApiParam(value = "关键词", required = false) @RequestParam(value = "keyword", required = false) String keyword,

            @ApiParam(value = "最低价格", required = false) @RequestParam(value = "minPrice", required = false) BigDecimal minPrice,

            @ApiParam(value = "最高价格", required = false) @RequestParam(value = "maxPrice", required = false) BigDecimal maxPrice,

            @ApiParam(value = "是否热门", required = false) @RequestParam(value = "isHot", required = false) Boolean isHot,

            @ApiParam(value = "是否推荐", required = false) @RequestParam(value = "isRecommended", required = false) Boolean isRecommended,

            @ApiParam(value = "代理ID", required = false) @RequestParam(value = "agentId", required = false) Long agentId,

            @ApiParam(value = "排序字段", required = false) @RequestParam(value = "sortBy", required = false) String sortBy,

            @ApiParam(value = "排序方向", required = false, defaultValue = "asc") @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,

            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        GameServiceQueryParam queryParam = new GameServiceQueryParam();
        queryParam.setCategoryId(categoryId);
        queryParam.setKeyword(keyword);
        queryParam.setMinPrice(minPrice);
        queryParam.setMaxPrice(maxPrice);
        queryParam.setIsHot(isHot);
        queryParam.setIsRecommended(isRecommended);
        queryParam.setAgentId(agentId);
        queryParam.setSortBy(sortBy);
        queryParam.setSortOrder(sortOrder);
        queryParam.setPage(page);
        queryParam.setPageSize(pageSize);

        return ApiResult.success(serviceService.getServiceList(queryParam));
    }

    /**
     * 获取游戏服务详情
     */
    @ApiOperation("获取游戏服务详情")
    @GetMapping("/{serviceId}")
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<GameServiceVO> getServiceDetail(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId) {
        return ApiResult.success(serviceService.getServiceDetail(serviceId));
    }

    /**
     * 搜索游戏服务
     */
    @ApiOperation("搜索游戏服务")
    @GetMapping("/search")
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<PageResult<GameServiceListVO>> searchServices(
            @ApiParam(value = "关键词", required = true) @RequestParam("keyword") String keyword,

            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(serviceService.searchServices(keyword, page, pageSize));
    }

    /**
     * 获取热门游戏服务列表
     */
    @ApiOperation("获取热门游戏服务列表")
    @GetMapping("/hot")
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<List<GameServiceListVO>> getHotServiceList(
            @ApiParam(value = "限制数量", required = false, defaultValue = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        return ApiResult.success(serviceService.getHotServiceList(limit));
    }

    /**
     * 获取推荐游戏服务列表
     */
    @ApiOperation("获取推荐游戏服务列表")
    @GetMapping("/recommended")
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<List<GameServiceListVO>> getRecommendedServiceList(
            @ApiParam(value = "限制数量", required = false, defaultValue = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        return ApiResult.success(serviceService.getRecommendedServiceList(limit));
    }

    /**
     * 创建游戏服务
     */
    @ApiOperation("创建游戏服务")
    @PostMapping
    @PreAuthorize("hasAnyRole('ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<Map<String, Long>> createService(@Validated @RequestBody GameServiceCreateParam param) {
        Long id = serviceService.createService(param, getCurrentUserId());
        Map<String, Long> result = new HashMap<>();
        result.put("id", id);
        return ApiResult.success("创建成功", result);
    }

    /**
     * 更新游戏服务
     */
    @ApiOperation("更新游戏服务")
    @PutMapping("/{serviceId}")
    @PreAuthorize("hasAnyRole('ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<Boolean> updateService(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId,

            @Validated @RequestBody GameServiceUpdateParam param) {
        return ApiResult.success("更新成功", serviceService.updateService(serviceId, param, getCurrentUserId()));
    }

    /**
     * 删除游戏服务
     */
    @ApiOperation("删除游戏服务")
    @DeleteMapping("/{serviceId}")
    @PreAuthorize("hasAnyRole('ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<Boolean> deleteService(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId) {
        return ApiResult.success("删除成功", serviceService.deleteService(serviceId, getCurrentUserId()));
    }

    /**
     * 更新游戏服务状态
     */
    @ApiOperation("更新游戏服务状态")
    @PutMapping("/{serviceId}/status")
    @PreAuthorize("hasAnyRole('ROLE_AGENT', 'ROLE_ADMIN')")
    public ApiResult<Boolean> updateServiceStatus(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId,

            @ApiParam(value = "状态", required = true) @RequestParam("status") String status) {
        return ApiResult.success("状态已更新", serviceService.updateServiceStatus(serviceId, status, getCurrentUserId()));
    }

    /**
     * 管理员设置热门游戏服务
     */
    @ApiOperation("管理员设置热门游戏服务")
    @PutMapping("/{serviceId}/hot")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResult<Boolean> adminSetHotService(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId,

            @ApiParam(value = "是否热门", required = true) @RequestParam("isHot") Boolean isHot) {
        return ApiResult.success("设置成功", serviceService.adminSetHotService(serviceId, isHot));
    }

    /**
     * 管理员设置推荐游戏服务
     */
    @ApiOperation("管理员设置推荐游戏服务")
    @PutMapping("/{serviceId}/recommended")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResult<Boolean> adminSetRecommendedService(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId,

            @ApiParam(value = "是否推荐", required = true) @RequestParam("isRecommended") Boolean isRecommended) {
        return ApiResult.success("设置成功", serviceService.adminSetRecommendedService(serviceId, isRecommended));
    }

    /**
     * 管理员删除游戏服务
     */
    @ApiOperation("管理员删除游戏服务")
    @DeleteMapping("/admin/{serviceId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResult<Boolean> adminDeleteService(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId) {
        return ApiResult.success("删除成功", serviceService.adminDeleteService(serviceId));
    }
}
